package com.shsubway.activity.staff;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.shsubway.R;
import com.shsubway.activity.manger.MakeQuestionActivity;
import com.shsubway.adapter.MyViewPagerAdapter;
import com.shsubway.base.BaseActivity;
import com.shsubway.fragment.QuestionFragment;
import com.shsubway.model.QuestionEntity;
import com.shsubway.model.StaffEntity;
import com.shsubway.utils.Constant;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;

/**
 * 查看题库
 */
public class LookQuestionsActivity extends BaseActivity {

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.my_vp)
    ViewPager myVp;
    Intent intent;
    Bundle bundle;
    int pos;
    QuestionEntity question;
    MyViewPagerAdapter myViewPagerAdapter;
    String[] titles = {};
    List<Fragment> fragmentList;
    @BindView(R.id.page_no)
    TextView pageNo;
    @BindView(R.id.btn_edit)
    ImageView btnEdit;
    List<QuestionEntity> lists;
    StaffEntity staffEntity;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_look_questions);
        ButterKnife.bind(this);
        initView();
        initEvent();
    }

    private void initEvent() {
        toolbar.setNavigationOnClickListener(v -> finish());

        myVp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                pos = position;
                pageNo.setText(getType() + (position + 1) + "/" + fragmentList.size());
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private String getType() {
        if (question.getQsttype() == 0) {
            return "单选题";
        } else if (question.getQsttype() == 1) {
            return "多选题";
        } else if (question.getQsttype() == 2) {
            return "判断题";
        }
        return null;
    }

    private void initView() {
        setTitleBar(toolbar, "学习");
        intent = getIntent();
        bundle = intent.getExtras();
        staffEntity= (StaffEntity) bundle.getSerializable(Constant.STAFFENTITY);
        question = (QuestionEntity) bundle.getSerializable(Constant.QUESTION);
        pos = bundle.getInt(Constant.QUESTION_NO);
        if (staffEntity.getStaff_permissions()!=1){
            btnEdit.setVisibility(View.GONE);
        }
        initData();
    }

    private void initData() {
        fragmentList = new ArrayList<>();
        BmobQuery<QuestionEntity> query = new BmobQuery<>();
        query.addWhereEqualTo("qsttype", question.getQsttype());
        query.setLimit(1000);
        query.findObjects(new FindListener<QuestionEntity>() {
            @Override
            public void done(List<QuestionEntity> list, BmobException e) {
                if (e == null) {
                    lists = new ArrayList<QuestionEntity>();
                    lists.addAll(list);
                    for (QuestionEntity questionEntity : list) {
                        QuestionFragment questionFragment = new QuestionFragment();
                        questionFragment.setQues(questionEntity, 1);
                        fragmentList.add(questionFragment);
                    }
                    pageNo.setText(getType() + (pos + 1) + "/" + fragmentList.size());

                    initAdapter();
                } else {
                    toast("查询数据失败。");
                }


            }
        });


    }

    private void initAdapter() {
        myViewPagerAdapter = new MyViewPagerAdapter(getSupportFragmentManager(), fragmentList, titles);
        myVp.setAdapter(myViewPagerAdapter);
        myVp.setCurrentItem(pos);
    }

    @OnClick(R.id.btn_edit)
    public void onClick() {
        bundle.putSerializable(Constant.QUESTION, lists.get(pos));
        startActivity(MakeQuestionActivity.class, bundle);
    }
}
